package com.gaotu.demo.transaction.direct;

import com.gaotu.demo.transaction.User;
import com.gaotu.demo.transaction.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.sql.*;

@Service
public class JdbcBasedDirectUserRepository implements UserRepository {
    @Autowired
    private DataSource dataSource;

    @Override
    public void save(User user) {
        Connection connection = null;
        try {
            connection = this.dataSource.getConnection();
            connection.setAutoCommit(false);
            connection.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
            connection.setReadOnly(false);

            PreparedStatement preparedStatement = connection
                    .prepareStatement("insert into user (user_name, pwd, create_time, update_time) value (?,?,?,?)",
                            Statement.RETURN_GENERATED_KEYS);
            preparedStatement.setString(1, user.getUserName());
            preparedStatement.setString(2, user.getPwd());
            preparedStatement.setDate(3, new java.sql.Date(user.getCreateTime().getTime()));
            preparedStatement.setDate(4, new java.sql.Date(user.getUpdateTime().getTime()));

            preparedStatement.execute();
            ResultSet generatedKeys = preparedStatement.getGeneratedKeys();
            if (generatedKeys.next()){
                user.setId(generatedKeys.getLong(1));
            }
            connection.commit();
        }catch (Exception e) {
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            if (connection != null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public User getByUserName(String name) {
        Connection connection = null;
        try {
            connection = this.dataSource.getConnection();
            connection.setReadOnly(true);

            PreparedStatement preparedStatement = connection
                    .prepareStatement("select id, user_name, pwd, create_time, update_time from user where user_name = ?");
            preparedStatement.setString(1, name);

            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                User user = new User();
                user.setId(resultSet.getLong(1));
                user.setUserName(resultSet.getString(2));
                user.setPwd(resultSet.getString(3));
                user.setCreateTime(resultSet.getDate(4));
                user.setUpdateTime(resultSet.getDate(5));
                return user;
            }

        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if (connection != null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        return null;
    }
}
